package centerberg.engine;

import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;

/**
 * The interface implemented by any object that wants to be simulated by the
 * physics engine.
 * 
 * @author Andreas Svanberg
 * @see PhysicsEngine
 * @see PhysicalRepresentation
 */
public abstract class PhysicsObject extends GameObject
{
    protected PhysicalRepresentation physicalRepresentation;
    
    protected PhysicsObject(Shape shape)
    {
        if (!(shape instanceof Rectangle2D || shape instanceof Ellipse2D))
            throw new IllegalArgumentException("Shape must be Rectangle2D or Ellipse2D");
            
        if (shape instanceof Ellipse2D)
        {
            Ellipse2D circle = (Ellipse2D)shape;
            if (circle.getHeight() != circle.getWidth())
                throw new IllegalArgumentException("Ellipses must have equal height and width");
        }
        
        physicalRepresentation = new PhysicalRepresentation(shape);
    }
    
    /**
     * Returns the physical representation of this physics object.
     * 
     * @return the physical representation
     */
    public PhysicalRepresentation getPhysicalRepresentation()
    {
        return physicalRepresentation;
    }
    
    public Vector getCenter()
    {
        Rectangle2D bounds = physicalRepresentation.getArea().getBounds2D();
        return new Vector(bounds.getCenterX(), bounds.getCenterY());
    }
    
    /**
     * Called when the physics engine moves this object.
     * Is generally used to synch the graphical representation of the object
     * with its physical.
     * 
     * @param dx how much moved in the x-axis
     * @param dy how much moved in the y-axis
     */
    public abstract void translated(double dx, double dy);
    
    /**
     * Called when this object collides with another object.
     * 
     * @param other the other object it collided with
     */
    public abstract void collided(PhysicsObject other);
}
